﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.Management;
using System.IO;

namespace BatteryStatusService
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void WriteBatteryStatus()
        {

            PowerStatus ps = SystemInformation.PowerStatus;
            string status =  ps.PowerLineStatus.ToString() + " " + ps.BatteryChargeStatus.ToString() + " " + (ps.BatteryLifePercent * 100).ToString() + "% " + DateTime.Now;

            // Specify the directory you want to manipulate.
            string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\BatteryStatus";

            try
            {
                // Determine whether the directory exists.
                if (!Directory.Exists(path))
                {
                    // Try to create the directory.
                    DirectoryInfo di = Directory.CreateDirectory(path);
                }
                System.IO.File.WriteAllText(path + @"\BatteryStatus.txt", status);
                label1.Text = status;
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
            }

           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //WriteBatteryStatus();
            timer1.Start();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
            notifyIcon1.BalloonTipText = "You have successfully minimized your form.";

            if (FormWindowState.Minimized == this.WindowState)
            {
                notifyIcon1.Visible = true;
                notifyIcon1.ShowBalloonTip(500);
                this.Hide();
            }
            else if (FormWindowState.Normal == this.WindowState)
            {
                notifyIcon1.Visible = false;
            }
        }

        private void notifyIcon1_MouseDoubleClick_1(object sender, MouseEventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            WriteBatteryStatus();
        }
    }
}
